<template>
  <el-row>
    <el-col :span="3">
      <div class="grid-content bg-purple">
        <RHTree
          :RHTree="RHTree"
          :titleClick="tableRefresh"
          ref="r_h_tree"
        ></RHTree>
      </div>
    </el-col>
    <el-col :span="21">
      <div class="grid-content bg-purple">
        <RHTable :RHTable="table" ref="r_h_table"></RHTable>
      </div>
    </el-col>
  </el-row>
</template>
<script>
import _ from "lodash";
import RHTree from "@/common/Tree/index.vue";
import RHTable from "@/common/RHTable/index.vue";

export default {
  name: "TreeTable_index",
  data() {
    return {
      table: {},
      tree: {
        nodeClick: this.nodeClick
      }
    };
  },
  methods: {
    nodeClick(data) {
      if (this.nodeClickCallBack(data,this.table.params)) {
        const key = _.keys(this.table.params);
        key.forEach((item, index) => {
          if(!_.isNil(data[item])){
            this.table.params[item] = data[item];
          }
        });
        this.tableRefresh(this.table.params);
      }
    },
    initialize(RHTable, RHTree) {
      const table = _.defaultsDeep(RHTable, this.table);
      const tree = _.defaultsDeep(RHTree, this.tree);
      this.table = table;
      this.tree = tree;
      setTimeout(() => {
        this.$refs.r_h_tree.initialize(tree);
        this.$refs.r_h_table.initialize(table);
      }, 0);
    },
    //重置表格数据
    tableRefresh(params) {
      this.$refs.r_h_table.reset(params);
    },
    treeRefresh(params, openH = true) {
      this.$refs.r_h_tree.refresh(params, openH);
    }
  },
  created() {
    const treeTemp = this.RHTree || {};
    const tableTemp = this.RHTable || {};
    this.initialize(tableTemp, treeTemp);
  },
  props: {
    RHTable: {},
    RHTree: {},
    nodeClickCallBack: {
      type: Function,
      default: function() {
        return true;
      }
    }
  },
  components: {
    RHTree,
    RHTable
  }
};
</script>

<style>
.el-tree .el-tree-node__content {
  height: 40px;
}

.el-row {
  margin-bottom: 20px;

  & :last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
